package com.weiwei.easyapiinterface.controller;

import cn.hutool.http.HttpUtil;
import com.weiwei.easyapiclientsdk.model.IPParams;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@RestController
@RequestMapping("/ip")
public class IPController {
    
    @PostMapping("/search")
    public String getIpAddressByPost(@RequestBody(required = false)IPParams ipParams, HttpServletRequest request) {
        String IPUrl = "https://api.vvhan.com/api/getIpInfo";
        HashMap<String, Object> paramMap = new HashMap<>();
        if (ipParams == null) {
           ipParams = new IPParams(); 
        }
        if (ipParams.getIp() == null || ("").equals(ipParams)) {
            return "{\"error\": \"参数不能为空\"}";
        }else{
            paramMap.put("city", ipParams.getIp());
        }
        return HttpUtil.get(IPUrl, paramMap);
    }
}
